package com.qlslylq.ad_x_sdk.view;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.qlslylq.ad.sdk.core.listener.BannerAdListener;
import com.qlslylq.ad.sdk.core.loader.BannerAdLoader;
import com.qlslylq.ad.sdk.core.rsp.AdError;
import com.qlslylq.ad.sdk.enums.AdSize;
import com.qlslylq.ad.sdk.enums.ErrorEnum;
import com.qlslylq.ad_x_sdk.comm.CommKey;
import com.qlslylq.ad_x_sdk.comm.CommRsp;
import com.qlslylq.ad_x_sdk.plugin.BasePlugin;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

/**
 * created by qlslylq <br/>
 * on 2022/5/23 <br/>
 */
public class BannerAdView extends LinearLayout implements PlatformView, MethodChannel.MethodCallHandler {

    private MethodChannel channel;

    private BannerAdLoader loader;
    private Integer posId;
    private Integer w;
    private AdSize adSize;

    public BannerAdView(final Context context, BinaryMessenger messenger, int id, Map<String, Object> req) {
        super(context);
        posId = (Integer) req.get(CommKey.JH_BANNER_POS_ID);
        w = (Integer) req.get(CommKey.W);
        Integer h = (Integer) req.get(CommKey.H);
        Integer gravity = (Integer) req.get(CommKey.GRAVITY);
        adSize = AdSize.getEnumByName((String) req.get(CommKey.AD_SIZE));

        if (posId == null) {
            posId = 0;
        }
        if (w == null || w == 0) {
            w = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        if (h == null || h == 0) {
            h = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        if (gravity == null) {
            gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        }
        setOrientation(LinearLayout.VERTICAL);
        setGravity(gravity);
        LayoutParams params = new LayoutParams(w, h);
        setLayoutParams(params);

        BannerAdView.this.channel = new MethodChannel(messenger, "com.qlslylq.ad-x-sdk/bannerAdView_" + id);
        BannerAdView.this.channel.setMethodCallHandler(BannerAdView.this);
    }

    public View getView() {
        return this;
    }

    public void dispose() {

    }

    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        Map<String, Object> req = (Map<String, Object>) call.arguments;

        if (call.method.equals("initLoader")) {
            initLoader((Activity) getContext(), result);
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("load")) {
            loader.load();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("show")) {
            loader.show();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("loadAndShow")) {
            loader.loadAndShow();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("setSlideIntervalTime")) {
            Integer slideIntervalTime = (Integer) req.get(CommKey.SLIDE_INTERVAL_TIME);
            if (slideIntervalTime != null) {
                loader.setSlideIntervalTime(slideIntervalTime);
                result.success(CommRsp.newInstance().putSuccess());
            } else {
                result.error(ErrorEnum.ERR_FAILD.getId() + "", "slideIntervalTime cannot be null", null);
            }
        } else if (call.method.equals("closeAd")) {
            loader.closeAd();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("destroyAd")) {
            loader.destroyAd();
            result.success(CommRsp.newInstance().putSuccess());
        }
    }

    private void initLoader(Activity context, MethodChannel.Result result) {
        loader = new BannerAdLoader(context, posId, this, w, adSize, new BannerAdListener() {
            public void onAdShow() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdShow");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdShowError(AdError error) {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdShowError").putError(error);
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdDismiss() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdDismiss");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdClick() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdClick");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onDislikeShow() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onDislikeShow");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onDislikeSelected(String value) {
                CommRsp rsp = CommRsp.newInstance().putEventName("onDislikeSelected").putDislikeValue(value);
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onDislikeDismiss() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onDislikeDismiss");
                BasePlugin.sendSuccessEvent(rsp);
            }
        });
    }

}
